package command.swing;

import javax.swing.*;
import java.awt.*;

public class SwingCommandExample {
    JFrame frame;
    JPanel panel;

    public static void main(String[] args) {
        SwingCommandExample example = new SwingCommandExample();
        example.go();
    }

    public void go() {
        frame = new JFrame();
        panel = new JPanel();

        // The GUI is the client
        // The buttons are the invokers
        JButton onButton = new JButton("On");
        JButton offButton = new JButton("Off");

        // The light is the receiver
        JLabel light = new JLabel("light");
        light.setOpaque(true);
        light.setBackground(Color.LIGHT_GRAY);

        onButton.addActionListener((event) -> light.setBackground(Color.YELLOW));
        offButton.addActionListener((event) -> light.setBackground(Color.LIGHT_GRAY));
        // set the frame properties
        frame.setContentPane(panel);
        panel.add(onButton);
        panel.add(light);
        panel.add(offButton);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 300);
        frame.setVisible(true);
    }
}
